﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace WebApp
{
    public partial class TeaInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT   TeaName, Gender, Class, Subject, Memo FROM      TeacherInfo  WHERE   (TeaNo = @TeaNo)";
            SqlParameter[] pms = new SqlParameter[1];
            pms[0] = new SqlParameter("@TeadNo", this.txtTeaNo.Text);
            SqlDataReader dr = SqlHelper.ExecuteReader(strSql, pms);
            if (dr.Read())
            {
                this.txtName.Text = dr["TeaName"].ToString();
                this.txtGender.Text = dr["Gender"].ToString();
                this.txtClass.Text = dr["Class"].ToString();
                this.txtsubject.Text = dr["Subject"].ToString();
                this.txtMemo.Text = dr["Memo"].ToString();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('已成功查询！')", true);

            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('查询失败！您尚未编辑您的个人基本信息！')", true);
            }
            dr.Close();
        }

        protected void btnUpdata_Click(object sender, EventArgs e)
        {
            try
            {

                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE  TeacherInfo ");
                SqlStringBuilder.AppendFormat("SET     TeaName = N'{0}', Gender = N'{1}', Class = N'{2}', Subject = N'{3}', Memo = N'{4}' ", this.txtName.Text, this.txtGender.Text, this.txtClass.Text, this.txtsubject.Text, this.txtMemo.Text);
                SqlStringBuilder.AppendFormat("WHERE   (TeaNo = N'{0}') ", this.txtTeaNo.Text);

                int iUpd = SqlHelper.ExecuteNonQuery(SqlStringBuilder.ToString());
                if (iUpd > 0)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('更新成功！已更新" + iUpd.ToString() + "条记录！')", true);
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = "更新失败！请重新编辑……具体原因：" + ex.Message;
            }
        }

        protected void btnSource_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT  * FROM     TeacherInfo";

            SqlDataReader dr = SqlHelper.ExecuteReader(strSql);
            this.GridView1.DataSource = dr;
            this.GridView1.DataBind();

            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('全部数据已读取完毕！')", true);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder sb = new StringBuilder(1024);
                sb.Append("INSERT INTO TeacherInfo ");
                sb.Append("                (TeaNo, TeaName, Gender,Class, Subject, Memo) ");
                sb.Append("VALUES   (@TeaNo,@TeaName,@Gender,@Class,@College,@Memo) ");
                SqlParameter[] pms = new SqlParameter[6];
                pms[0] = new SqlParameter("@TeaNo", this.txtTeaNo.Text);
                pms[1] = new SqlParameter("@TeaName", this.txtName.Text);
                pms[2] = new SqlParameter("@Gender", this.txtGender.Text);
                pms[3] = new SqlParameter("@Class", this.txtClass.Text);
                pms[4] = new SqlParameter("@Subject", this.txtsubject.Text);
                pms[5] = new SqlParameter("@Memo", this.txtMemo.Text);
                int intResult = SqlHelper.ExecuteNonQuery(sb.ToString(), pms);
                if (intResult > 0)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('添加成功！已新增" + intResult.ToString() + "条记录！')", true);

                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = "添加失败！该教师信息已存在，请重新添加……具体原因：" + ex.Message;

            }
        }

        protected void btnDelect_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("DELETE FROM TeacherInfo ");
                SqlStringBuilder.AppendFormat("WHERE   (TeaNo = N'" + this.txtTeaNo.Text + "') ");

                int iDelect = SqlHelper.ExecuteNonQuery(SqlStringBuilder.ToString());
                if (iDelect > 0)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('删除成功！已成功删除了" + iDelect.ToString() + "条记录！')", true);
                }
            }
            catch (Exception ex)
            {
                this.lblInfo.Text = "删除失败！请重新删除信息……具体原因：" + ex.Message;
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            Session["Theme"] = this.DropDownList1.SelectedValue.ToString();
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] != null)
                this.Theme = Session["Theme"].ToString();
        }
    }
}